//
//  IndexPageTwo.m
//  FHC
//
//  Created by alex on 15/2/11.
//  Copyright (c) 2015年 alex. All rights reserved.
//

#import "IndexPageTwo.h"
#import "IndexViewCell.h"
#import "IndexPageNewModel.h"
#import "XXTool.h"

#import "RXMLExhibitorIndexModel.h"

@interface IndexPageTwo ()
{
    IndexPageNewModel *useData;
    NSString *useType;
    BOOL isHeadFresh; //解决下拉刷新整个页面闪烁的问题
}
@end

@implementation IndexPageTwo

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.frame=CGRectMake(self.view.frame.origin.x,
                               self.view.frame.origin.y,
                               self.view.frame.size.width,
                               self.view.frame.size.height-64-49);
    
    _tablelist = [[UITableView alloc] initWithFrame:CGRectMake(0
                                                               , 0
                                                               , self.view.frame.size.width
                                                               , self.view.frame.size.height)];
    _tablelist.delegate =self;
    _tablelist.dataSource = self;
    [_tablelist setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    MJRefreshNormalHeader *useHead = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    [useHead setTitle:NSLocalizedString(@"下拉刷新", nil) forState:MJRefreshStateIdle];
    [useHead setTitle:NSLocalizedString(@"刷新中...", nil) forState:MJRefreshStateRefreshing];
    [useHead setTitle:NSLocalizedString(@"松开立即刷新", nil) forState:MJRefreshStatePulling];
    _tablelist.mj_header = useHead;
    
    MJRefreshBackNormalFooter *useFooter = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerReFeren)];
    [useFooter setTitle:NSLocalizedString(@"刷新中...", nil) forState:MJRefreshStateRefreshing];
    [useFooter setTitle:NSLocalizedString(@"上拉刷新", nil) forState:MJRefreshStateIdle];
    [useFooter setTitle:NSLocalizedString(@"松开立即刷新", nil) forState:MJRefreshStatePulling];
    _tablelist.mj_footer = useFooter;
    [self.view addSubview:_tablelist];
    
    //给tablelist加整体背景
    //----------------------------------------------------------------
    /*
    UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"PPKC下拉背景"]];
    img.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    
    [_tablelist setBackgroundView:img];
     */
    isHeadFresh = NO;
    
    [self getData];
    
}

-(instancetype)initWithNewsType:(NSString *)type
{
    self = [super init];
    if (self)
    {
        useType = type;
    }
    return self;
}


-(void)footerReFeren
{
    [IndexPageNewModel getMoreIndexNewsWithType:useType nowNewsCount:useData.list.count
                                     completion:^(IndexPageNewModel *data) {
                                         if (data!=nil)
                                         {
                                             if (data.list.count==0)
                                             {
                                                 [SVProgressHUD showInfoWithStatus:NSLocalizedString(@"暂无更多新闻", nil)];
                                             }
                                             else
                                             {
                                                 [useData.list addObjectsFromArray:data.list];
                                                 [_tablelist reloadData];
                                             }
                                         }
                                         [_tablelist.mj_footer endRefreshing];
                                     }];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}


-(void)getData
{
    [IndexPageNewModel getIndexNewsWithType:useType
                              beforeGetData:^{
                                  if (isHeadFresh)
                                  {
                                      
                                  }
                                  else
                                  {
                                      [self.view startLoading];
                                  }
                              } completion:^(IndexPageNewModel *data) {
                                  isHeadFresh = YES;
                                  if (data!=nil)
                                  {
                                      useData = data;
                                      [_tablelist reloadData];
                                  }
                                  [_tablelist.mj_header endRefreshing];
                                  [self.view stopLoading];
                              }];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    IndexViewCellModel *temp = useData.list[indexPath.row];
    NSString *ss=temp.contentabout;
//    NSString *encodedValue = [temp.contentabout stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [WebController pushWebControllerWithSourceVC:self requestString:temp.contentabout mapTitle:temp.news_title];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return useData.list.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [UIScreen mainScreen].bounds.size.width*170/375;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString* reuseIdentifier = @"IndexViewCell";
    IndexViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if(cell==nil)
    {
        cell = [[IndexViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.clipsToBounds = YES;
    }
    IndexViewCellModel *temp = useData.list[indexPath.row];
    [cell initImageView:temp.photo_url andLabelView:temp.news_title];
    
    return cell;
}




@end
